package edu.rakovic.elearning.pages;

import java.util.Collection;
import java.util.LinkedList;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;

import edu.rakovic.elearning.enums.Role;
import edu.rakovic.elearning.interfaces.Application;
import edu.rakovic.elearning.interfaces.Course;
import edu.rakovic.elearning.interfaces.User;
import edu.rakovic.elearning.service.Applications;
import edu.rakovic.elearning.service.Courses;

public class ViewTeachingCourses {
	
	@Inject
	@Property
	private Courses courses;
	
	@Inject
	@Property
	private Applications applications;
	
	@Property
	private Course currentCourse;
	
	@Property
	private Application currentApplication;
	
	@Persist
	private String courseName;
	
	@InjectPage
	private AddContent contentPage;
	
	
	@SessionState
	private User user;

	private boolean userExists;
	
	public User getUser() {
		return user;
	}
	
	Object onActivate() {
		if (!userExists) return Login.class;
		if (getUser().getRole()!= Role.TEACHER) return Index.class;
		return null;
	}
	
	
	public Collection<Course> getTeachingCollection() {
		return courses.retrieveTeachingCourses(getUser().getUserName());
	}
	
	public Collection<Application> getStudents() {
		return applications.retrieveCourseApplications(courseName);
	}
	
	void onActionFromStudents(String courseName){
		this.courseName = courseName;
	}
	
	
	// Event handler for action link - delete user
	void onActionFromDelete(String courseName){
		
		Course course = courses.retreiveSingleCourse(courseName);
		course.removeCourse(courses, applications);
	}
	
	//Cancel single student course application
	void onActionFromCancel(String appId){
		Application appl = applications.retreiveSingleApplication(appId);
		//using "database applications" to perform application removal.
		appl.cancelApplication(applications);

	}
	
	void onActionFromProfile(String userName){
		System.out.println("profile");
	}
	
	Object onActionFromContent(String courseName){
		
		Course course = courses.retreiveSingleCourse(courseName);
		contentPage.setCourse(course);
		return contentPage;
		
	}
	

	


}
